<template>
    <div>
        <div style="display: flex;justify-content: space-between;margin-top: 10px">
            <el-button size="medium" icon="el-icon-plus" type="primary" @click="showAddSalaryView">添加工资账套</el-button>
            <el-button size="medium" icon="el-icon-refresh" type="success" @click="initSalaries"></el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table :data="salaries" border stripe
                      v-loading="loading"
                      element-loading-text="正在加载..."
                      element-loading-spinner="el-icon-loading"
                      element-loading-background="rgba(0, 0, 0, 0.8)">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column width="120" prop="name" label="账套名称"></el-table-column>
                <el-table-column width="70" prop="basicsalary" label="基本工资"></el-table-column>
                <el-table-column width="70" prop="trafficsalary" label="交通补助"></el-table-column>
                <el-table-column width="70" prop="lunchsalary" label="午餐补助"></el-table-column>
                <el-table-column width="70" prop="bonus" label="奖金"></el-table-column>
                <el-table-column width="100" prop="createdate" label="启用时间" :formatter="createDate"></el-table-column>
                <el-table-column label="养老金" align="center">
                    <el-table-column width="70" prop="pensionper" label="比率"></el-table-column>
                    <el-table-column width="70" prop="pensionbase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="医疗保险" align="center">
                    <el-table-column width="70" prop="medicalper" label="比率"></el-table-column>
                    <el-table-column width="70" prop="medicalbase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="公积金" align="center">
                    <el-table-column width="70" prop="accumulationfundper" label="比率"></el-table-column>
                    <el-table-column width="70" prop="accumulationfundbase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button style="padding: 7px" @click="showEditSalaryView(scope.row)" size="mini">编辑</el-button>
                        <el-button style="padding: 7px" type="danger" @click="deleteSalary(scope.row)" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                :title="dialogTitle"
                :visible.sync="dialogVisible"
                width="50%">
            <div style="display: flex;justify-content: space-around;align-items: center">
                <el-steps direction="vertical" :active="activeItemIndex">
                    <el-step :title="itemName" v-for="(itemName,index) in salaryItemName" :key="index"></el-step>
                </el-steps>
                <el-input v-model="salary[name]" :placeholder="'请输入'+salaryItemName[index]+'...'"
                          v-for="(value,name,index) in salary"
                          :key="index" v-show="activeItemIndex==index" style="width: 200px"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="preStep">{{activeItemIndex==10?'取消':'上一步'}}</el-button>
                <el-button type="primary" @click="nextStep">{{activeItemIndex==10?'完成':'下一步'}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "SalSob",
        data:function () {
            return {
                loading: false,
                salaries: [],
                dialogVisible: false,
                dialogTitle: '',
                activeItemIndex: 1,
                salaryItemName: [
                    '基本工资',
                    '交通补助',
                    '午餐补助',
                    '奖金',
                    '养老金比率',
                    '养老金基数',
                    '医疗保险比率',
                    '医疗保险基数',
                    '公积金比率',
                    '公积金基数',
                    '账套名称'
                ],
                salary: null
            }
        },
        methods:{
            initSalaries:function () {
                this.loading = true
                this.$getRequest('/vhr/salary')
                    .then(res => {
                        this.loading = false
                        if (res) {
                            console.log(res);
                            this.salaries = res.obj.records
                        }
                    })
            },
            showEditSalaryView(row){
                this.activeItemIndex = 0
                this.dialogTitle = '修改工资账套';
                this.dialogVisible = true;
                this.salary = {
                    basicsalary: row.basicsalary,
                    trafficsalary: row.trafficsalary,
                    lunchsalary: row.lunchsalary,
                    bonus: row.bonus,
                    pensionper: row.pensionper,
                    pensionbase: row.pensionbase,
                    medicalper: row.medicalper,
                    medicalbase: row.medicalbase,
                    accumulationfundper: row.accumulationfundper,
                    accumulationfundbase: row.accumulationfundbase,
                    name: row.name,
                    id: row.id
                }
            },
            deleteSalary(row){
                this.$confirm('此操作将删除【' + row.name + '】账套，是否继续？', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定'
                }).then(() => {
                    this.$deleteRequest("/vhr/salary/" + row.id).then(resp => {
                        if (resp) {
                            this.initSalaries()
                        }
                    })
                })
            },
            showAddSalaryView(){
                //数据初始化
                this.salary = {
                    basicsalary: 0,
                    trafficsalary: 0,
                    lunchsalary: 0,
                    bonus: 0,
                    pensionper: 0,
                    pensionbase: 0,
                    medicalper: 0,
                    medicalbase: 0,
                    accumulationfundper: 0,
                    accumulationfundbase: 0,
                    name: '默认名称'
                }
                this.dialogTitle = '添加工资账套';
                this.activeItemIndex = 0;
                this.dialogVisible = true;
            },
            createDate: function (cellValue) {
                const date = new Date(cellValue.createdate);
                if (date){
                    const month = (date.getMonth() + 1).toString().padStart(2, '0');
                    const strDate = date.getDate().toString().padStart(2, '0');
                    return `${date.getFullYear()}-${month}-${strDate}`
                }
            },
            preStep(){
                if (this.activeItemIndex == 0) {
                    return;
                } else if (this.activeItemIndex == 10) {
                    //关闭对话框
                    this.dialogVisible = false;
                    return;
                }
                this.activeItemIndex--
            },
            nextStep(){
                if (this.activeItemIndex == 10) {
                    if (this.salary.id) {
                        this.$putRequest('/vhr/salary', this.salary)
                            .then(res => {
                                if (res) {
                                    this.initSalaries()
                                    this.dialogVisible = false
                                }
                            })
                    } else {
                        this.$postRequest('/vhr/salary', this.salary)
                            .then(res => {
                                if (res) {
                                    this.initSalaries()
                                    this.dialogVisible = false
                                }
                            })
                    }
                    return;
                }
                this.activeItemIndex++
            }
        },
        mounted() {
            this.initSalaries()
        }
    }
</script>

<style scoped>

</style>